package my.example.samplegridview;

import java.io.BufferedInputStream;
import java.util.Vector;

import my.example.samplegridview.modal.product;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProductAdapter extends BaseAdapter {
    private Context mContext;
    private Vector<product> products;
    private Activity activity;
    private int requestcode;

    public ProductAdapter(Context c , Vector<product> products , Activity activity) {
        mContext = c;
        this.products = products;
        this.activity = activity;
        if(activity instanceof samplegridview){
        	requestcode = samplegridview.requestcode;
		}
		else if(activity instanceof whatsnew){
			requestcode = whatsnew.requestcode;
		}
    }

    @Override
	public int getCount() {
        return products.size();
    }

    @Override
	public Object getItem(int position) {
        return null;
    }

    @Override
	public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
	public View getView(int position, View convertView, ViewGroup parent) {
    	
    	final product p = products.get(position);
    	
        LinearLayout productView = null;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
        	LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	productView = (LinearLayout) li.inflate(R.layout.product, null);
        } else {
        	productView = (LinearLayout) convertView;
        }

        BufferedInputStream buf;
		//Set Image
		//ImageView iv = (ImageView)productView.findViewById(R.id.productimage);
		
        LinearLayout productViewMain = (LinearLayout)productView.findViewById(R.id.productMainView);
		if(p.getImage().length() > 0){
			
			int indexer = p.getImage().lastIndexOf("/");
			String imageName = p.getImage().substring(indexer+1);
			
			Drawable d = Drawable.createFromPath(mContext.getApplicationInfo().dataDir + "/files/" + imageName);
			productViewMain.setBackgroundDrawable(d);
		}
		
		//Set Product Name
		TextView tv = (TextView)productView.findViewById(R.id.productname);
		tv.setText(p.getProdescription().getName());
		
		//Set Product Price
		tv = (TextView)productView.findViewById(R.id.productprice);
		tv.setText("RM " + p.getPrice() );//+ p.getUnit());
		
		productViewMain.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(activity,productdescription.class);
				i.putExtra("id", p.getProduct_id());
				i.putExtra("type", "add");
				activity.startActivityForResult(i,requestcode);
			}
		});
		
		//View Reviews
        Button b = (Button)productView.findViewById(R.id.productViewReviews);
        String totalReviews = " ( "+ String.valueOf(viewreview.datainitialization(mContext, p.getProduct_id()).size()) + " ) " ;
        String text = "Reviews" + totalReviews;
        b.setText(text);
        b.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent i2 = new Intent(activity,viewreview.class);
        		i2.putExtra("product_id", p.getProduct_id());
        		activity.startActivity(i2);
			}
        	
        });
        
        //Add Reviews
        b = (Button)productView.findViewById(R.id.productAddReviews);
        b.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent i3 = new Intent(activity,addreview.class);
        		i3.putExtra("product_id", p.getProduct_id());
        		activity.startActivity(i3);
        		}
        });
        
        if(activity instanceof productdiscount){
        	countdowntimer cdt = (countdowntimer)productView.findViewById(R.id.productDiscountCounter);
        	cdt.setVisibility(View.VISIBLE);
            cdt.startTimer(30000);
		}
        
        
        
        return productView;
    }
}

